package com.fckbug.action.user;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.fckbug.service.QAService;
import com.fckbug.util.SpringContextHolder;
import com.fckbug.util.WebappUtil;
import com.fckbug.vos.Acc;
import com.mysql.jdbc.StringUtils;

/**
 * ajax修改回答的mark次数
 * @author think
 *
 */
public class AlterQTimeAction extends Action{
	QAService qaService = (QAService)SpringContextHolder.getBean("qaService");
	
	public ActionForward execute(ActionMapping mapping, ActionForm form,
	            HttpServletRequest request, HttpServletResponse response) throws Exception {
		String resultStr = "";
		String qId = request.getParameter("qid");
		String increment = request.getParameter("increment");
		if(!increment.equals("1") && !increment.equals("-1") || StringUtils.isNullOrEmpty(qId)) {
			resultStr = "参数有误";
		} else {
			Acc acc = WebappUtil.getLoginUser(request);
			if(acc == null) {
				resultStr = "请先登录";
			} else {
				int result = qaService.alterQTime(acc.getId(), qId, increment);
				if(result==1) {
					resultStr = "success";
				} else if(result==0){
					resultStr = "您已经投票过";
				} else {
					resultStr = "投票失败";
				}
			}
		}
		response.setCharacterEncoding("utf-8");
		PrintWriter pw = response.getWriter();
		pw.print(resultStr);
		return null;
	}
}

